﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Comissoes.JQGridObject;
using Comissoes.Models;
using System.Web;
using Comissoes.Utilities;

namespace Comissoes.Controllers
{
    public class CondicoesPagamentoController : AppController
    {
        private xcontEntities db = new xcontEntities();
        
        public ActionResult Index()
        {
            return View();
        }        

        // Monta a lista de tipos de produtos
        public JsonResult List(String sidx, String sord, Int32 page, Int32 rows, Boolean _search, String searchField, String searchString, String searchOper)
        {
            try
            {
                var q = from c in db.CONDICOES_PAGAMENTO
                        select new
                        {
                            c.COD_CDPG,
                            c.DESC_CDPG,
                            TIPO_CDPG = c.TIPO_CDPG == "A" ? "Crediário" : "Comercial"
                        };

                JQGridBuilder jbuilder = new JQGridBuilder(q.AsQueryable(), page, sidx, sord, searchField, searchString, searchOper, "COD_CDPG", _search, rows);

                return Json(jbuilder.GetObject(), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Details(int id)
        {
            return View(new CondicoesPagamentoModel(id));
        }

        public ActionResult Edit(Int32? id)
        {
            return View(new CondicoesPagamentoModel(id));
        }

        [HttpPost]
        public ActionResult Edit(Int32? id, FormCollection form)
        {
            return Json(new CondicoesPagamentoModel(id).Update(form), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult Delete(int id)
        {
            return Json(new CondicoesPagamentoModel(id).Delete(), JsonRequestBehavior.AllowGet);
        }
        
        public JsonResult DiasCondicaoList(String sidx, String sord, Int32 page, Int32 rows, Boolean _search, String searchOper, String searchField, String searchString, Int32? COD_CDPG)
        {
            try
            {
                var q = from c in db.DIAS_CONDICAO_PAGAMENTO
                        where c.CONDICOES_PAGAMENTO.COD_CDPG == COD_CDPG
                        select new
                        {
                            act = "",
                            c.ID_DICP,
                            c.DIA_DICP
                        };

                JQGridBuilder jbuilder = new JQGridBuilder(q, page, sidx, sord, searchField, searchString, searchOper, "ID_DICP", _search, rows);

                return Json(jbuilder.GetObject(), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public ActionResult DiaCondicaoUpdate(FormCollection form)
        {
            return Json(new CondicoesPagamentoModel(Convertions.Inteiro(form["COD_CDPG"])).DiaCondicaoUpdate(form), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult DiaCondicaoDelete(Int32 ID_DICP, Int32 COD_CDPG)
        {
            return Json(new CondicoesPagamentoModel(COD_CDPG).DiaCondicaoDelete(ID_DICP), JsonRequestBehavior.AllowGet);
        }
    }
}
